<?php
class PersonTag_Controller extends AdminController {

    public $DISPLAY_PERSON_LIST_UUID = 'admin/personTag/display/personList';

    public function __construct() {
        parent::__construct('personTag');
        $this->setDefaultListOrder('id');
    }

    function all() {
        $this->initPagination('person_tag');

        $this->viewData['personTagList'] = $this->dao->queryAll(
                $this->orderLimitAndOffset('SELECT * FROM person_tag'));

        $this->renderSubview('admin/personTag/personTagList');
    }

    function display() {
        $id = $this->input->get('id');

        $personTag = $this->dao->getById('person_tag', $id);
        if (! $personTag) {
            $this->redirectToList('error.recordNotFound');
        }
        $this->viewData['personTag'] = $personTag;

        // Read list of persons with this tag
        $this->viewData['personList'] = $this->dao->queryAll(sql::orderBy($this->DISPLAY_PERSON_LIST_UUID,
                'SELECT DISTINCT p.* FROM person p ' .
                'JOIN person_tag_asoc tag ON p.id = tag.person_id ' .
                'WHERE tag.person_tag_id = :id', 'last_name'), array('id' => $id));

        // list of persons without this tag
        $personOptions = $this->dao->queryOptions(
                'SELECT p.id, p.first_name, p.last_name, p.birth_date FROM person p ' .
                'WHERE p.id NOT IN (' .
                'SELECT tag.person_id ' .
                'FROM person_tag_asoc tag ' .
                'WHERE tag.person_tag_id = :id)', array('id' => $id), array(' ', '()'));
        $this->viewData['personOptions'] = $personOptions;
        $this->viewData['formData'] = array('id' => $id);

        $this->renderSubview('admin/personTag/personTagDisplay');
    }

    function edit() {
        $id = $this->input->get('id');
        $personTag = array();

        if (valid::isId($id)) {
            $personTag = $this->dao->getById('person_tag', $id);
            if (! $personTag) {
                $this->msg->error('error.recordNotFound');
                $this->redirect('/admin/personTag/all');
            }
        }
        else {
            // Set default values for a new record here
        }

        $this->loadFormData();

        $this->viewData['personTag'] = $personTag;
        $this->renderSubview('admin/personTag/personTagEdit');
    }

    protected function loadFormData() {
    }

    function save() {
        $personTag = $this->postToDbValues();
        $id = $personTag['id'];
        $this->viewData['personTag'] = $personTag;

        // Basic validation
        $validator = new EntityValidator($personTag, 'personTag', $this->msg);
        $validator->required('name');

        if ($this->msg->hasErrors()) {
            $this->loadFormData();
            $this->publishErrors('admin/personTag/personTagEdit');
            return;
        }

        // Database validation - unique name
        if ($this->dao->alreadyExists('person_tag', 'name', $personTag, 'id')) {
            $this->msg->error('personTag.error-alreadyExists', $personTag['name']);
            $this->loadFormData();
            $this->publishErrors('admin/personTag/personTagEdit');
            return;
        }

        // Insert or update
        if (sql::isPersistent($personTag)) {
            $this->dao->update('person_tag', $personTag);
            $this->msg->info('personTag.editSuccess', $personTag['name']);
        }
        else {
            $this->dao->insert('person_tag', $personTag);
            $this->msg->info('personTag.createSuccess', $personTag['name']);
        }

        $this->ajaxSuccessOrRedirect('/admin/personTag/all', $personTag);
    }

    function delete() {
        $id = $this->input->get('id');

        if (! valid::isId($id)) {
            $this->msg->error('error.invalidId');
            $this->redirect('/admin/personTag/all');
        }

        // Basic validation
        $personTag = $this->dao->getById('person_tag', $id);
        if (! $personTag) {
            $this->msg->error('error.recordNotFound');
            $this->redirect('/admin/personTag/all');
        }

        $this->dao->delete('person_tag', $id);

        $this->msg->error('personTag.deleteSuccess', $personTag['name']);
        $this->redirect('/admin/personTag/all');
    }

    function addPerson() {
        $id = $this->input->post('id');
        $personId = $this->input->post('person_id');

        if (! valid::isId($id) || ! valid::isId($personId)) {
            $this->redirectToList('error.invalidId');
        }

        $personTag = $this->dao->getById('person_tag', $id);
        if (! $personTag) {
            $this->redirectToList('error.recordNotFound');
        }

        $person = $this->dao->fetchPersonById($personId);
        if (! $person) {
            $this->msg->error('error.recordNotFound');
            $this->redirect('/admin/personTag/display?id=' . $id);
        }

        $this->dao->insert('person_tag_asoc', array('person_id' => $personId, 'person_tag_id' => $id));

        $this->msg->info('personTag.addPersonSuccess', $personTag['name'], format::personFullName($person));
        $this->redirect('/admin/personTag/display?id=' . $id);
    }
}
